package com.xzbd.gef.controller;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.xzbd.core.gef.Node;
import com.xzbd.editors.MultiLineCellEditor;
import com.xzbd.gef.model.text.NotesNode;
import com.xzbd.gef.policy.AppDeletePolicy;
import com.xzbd.gef.view.NotesNodeFigure;
import com.xzbd.listeners.PropListener;

public class NotesNodePart extends AppAbstractEditPart implements NodeEditPart{

	private DirectEditManager directManager;
	
	@Override
	protected IFigure createFigure() {
		IFigure figure = new NotesNodeFigure();
		return figure;
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.COMPONENT_ROLE,new AppDeletePolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,new NotesDirectEditPolicy());
	}
	
	public void refreshVisuals(){
		NotesNodeFigure figure = (NotesNodeFigure)getFigure();
		NotesNode model = (NotesNode)getModel();
		
		figure.setNotes(model.getNotes());
		figure.setLayout(model.getLayout());
		figure.setColor(model.getColor());
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals(PropListener.PROPERTY_COLOR))refreshVisuals();
		if(evt.getPropertyName().equals(PropListener.PROPERTY_LAYOUT)) refreshVisuals();
		if(evt.getPropertyName().equals(NotesNode.PROPERTY_NOTES)) refreshVisuals();
	}
	
	public void performRequest(Request req) {
		if(req.getType().equals(RequestConstants.REQ_OPEN)) {
			try {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				page.showView(IPageLayout.ID_PROP_SHEET);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
		if (req.getType().equals(RequestConstants.REQ_DIRECT_EDIT)
				|| req.getType().equals(RequestConstants.REQ_OPEN)) {
			performDirectEdit();
			return;
		}
		//super.performRequest(req);
	}

	private void performDirectEdit() {
		if (directManager == null) {
			directManager = new NotesDirectEditManager();
		}
		directManager.show();
	}
	
	/**
	 * DirectEditManager
	 */
	private class NotesDirectEditManager extends DirectEditManager {

		public NotesDirectEditManager() {
			super(NotesNodePart.this, MultiLineCellEditor.class,
					new NoteCellEditorLocator());
		}

		protected void initCellEditor() {
			getCellEditor().setValue(((NotesNode) getModel()).getNotes());
			Text text = (Text) getCellEditor().getControl();
			text.selectAll();
		}
	}

	/**
	 * CellEditorLocator
	 */
	private class NoteCellEditorLocator implements CellEditorLocator {
		public void relocate(CellEditor celleditor) {
			IFigure figure =  getFigure();
			Text text = (Text) celleditor.getControl();
			Rectangle rect = figure.getBounds().getCopy();
			figure.translateToAbsolute(rect);
			//text.setBounds(rect.x + 5, rect.y + 5, rect.width - 5, rect.height - 5);
			text.setBounds(rect.x + 5, rect.y + 5, 180, 50);
		}
	}

	/**
	 * DirectEditCommand
	 */
	private class DirectEditCommand extends Command {

		private String oldName;

		private String newName;

		public void execute() {
			NotesNode model = (NotesNode) getModel();
			oldName = model.getNotes();
			model.setNotes(newName);
		}

		public void setName(String name) {
			newName = name;
		}

		public void undo() {
			NotesNode model = (NotesNode) getModel();
			model.setNotes(oldName);
		}
	}

	/**
	 * DirectEditPolicy
	 */
	private class NotesDirectEditPolicy extends DirectEditPolicy {

		protected Command getDirectEditCommand(DirectEditRequest request) {
			DirectEditCommand command = new DirectEditCommand();
			command.setName((String) request.getCellEditor().getValue());
			return command;
		}

		protected void showCurrentEditValue(DirectEditRequest request) {
		}
	}
	
	
	
	
	
	
	
	public List<Node> getModelChildren() {
		return new ArrayList<Node>();
	}

	@Override
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ConnectionAnchor getTargetConnectionAnchor(Request arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
